package com.itheima.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.bean.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {

    private UserService service = new UserServiceImpl();
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        ServletInputStream is = request.getInputStream();
        ObjectMapper mapper = new ObjectMapper();
        User user = mapper.readValue(is, User.class);

        System.out.println(user);
        User u = service.login(user);
        if(u != null){

            request.getSession().setAttribute("username",u.getUsername());
            response.getWriter().write("true");
        }else {
            response.getWriter().write("false");
        }

    }




}
